local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());
local AudioDS=CommonOHOH.config.Game.AudioDS
local MonsterAudioCd=CommonOHOH.config.Game.MonsterAudioCd
local Client_AudioManager = _G.ClientOHOH.load.Client_AudioManager();
local Tool=CommonOHOH.load.MonsterTool()

function M.new(self,...)
	return M(self,...);
end;


function M.ctor(self, parent)
	M.super.ctor(self, parent);
    
    self.cfg=self.parent:GetCfg()

    local data=Tool.GetMonsteArtByMid(self.cfg.id)
    
    self.HurtAudo= Client_AudioManager:LoadSound(data.audio.hurt)
    self.DieAudo= Client_AudioManager:LoadSound(data.audio.die)
    
    self.parent.HurtEvent:Connect(function(damge,attacker)
        if self.parent.attribute.Hp>0 and self:IsPlayAudio(self.parent:GetPosition()) then
            Client_AudioManager:PlayGloble(self.HurtAudo)
        end
    end)

    -- self.parent.DieEvent:Connect(function(damge,attacker)
    --     if self:IsPlayAudio(self.parent:GetPosition()) then
    --         Client_AudioManager:PlayGloble(self.DieAudo)
    --     end
    -- end)
    self.parent:AttributeValueChanged("DeadTime",function()
        if self:IsPlayAudio(self.parent:GetPosition()) then

            Client_AudioManager:PlayGloble(self.DieAudo)
        end
    end)

end;

function M.dtor(self)
	M.super.dtor(self);
end

-- 避免死亡音效太频繁
function M.IsPlayAudio(self,pos)
    if not self.lastTime then
        self.lastTime=os.clock()
        return true
    elseif os.clock()- self.lastTime>MonsterAudioCd then
        self.lastTime=os.clock()
        return true
    else
        return false
    end
end

return M